package com.doform.android.xianggucheng.holder;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/5/12.
 */

public class ViewHolder {

    private final SparseArray<View> mViews;
    private  View mConvertView;

    public ViewHolder(Context context, ViewGroup root, int layoutId, int position) {
        this.mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId,root,false);
        mConvertView.setTag(this);
    }

    /**
     * @param context
     * @param convertView
     * @param root
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView, ViewGroup root, int layoutId, int position){
        if (convertView == null){
            return new ViewHolder(context,root,layoutId,position);
        }
        return (ViewHolder) convertView.getTag();
    }


    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if (view == null){
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T) view;
    }

    public View getConvertView(){
        return mConvertView;
    }

}
